<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>计数器</title>
</head>

<body>
    <!-- 要挂载的节点 -->
    <div id="root"></div>
    <!-- 引入依赖 -->
    <script src="../lib/react.js"></script>
    <script src="../lib/react-dom.js"></script>
    <script src="../lib/babel.js"></script>
    <!-- 设置类型为text/babel 否则浏览器无法解析jsx -->
    <script type="text/babel">
        /* 点击实现显示隐藏 使用react实现 vue中v-if和v-show */

        // 1.定义app组件
        class App extends React.Component {
            constructor() {
                super()
                this.state = {
                    message: "Hello React 显示中。。。",
                    isShow: true,
                    title: "学习react"
                }
                this.changeShow = this.changeShow.bind(this)
            }

            changeShow() {
                this.setState({
                    isShow: !this.state.isShow
                })
            }

            render() {
                const { message, isShow, title } = this.state
                let showDOM = null
                /* 使用if实现 */
                if (isShow) {
                    showDOM = <h2>{message}</h2>
                } else {
                    showDOM = <h2>隐藏中。。。</h2>
                }

                return (
                    <div>
                        <button onClick={this.changeShow}>切换</button>
                        {showDOM}
                        {isShow && <h2>{message}</h2>}

                        {/* v-show效果实现 */}
                        <h2 style={{ display: isShow ? 'block' : 'none' }}>{title}</h2>
                    </div>
                )
            }
        }


        // 2.创建root并且渲染App组件
        const root = ReactDOM.createRoot(document.querySelector("#root"))
        root.render(<App />)
    </script>
</body>

</html>